package com.sunagy.demand.backend.service.impl;

import com.sunagy.demand.backend.VO.request.DictTypeAddRequestVO;
import com.sunagy.demand.backend.VO.request.DictValueAddRequestVO;
import com.sunagy.demand.backend.VO.request.DictValueEditRequestVO;
import com.sunagy.demand.backend.VO.response.DictTypeResponseVO;
import com.sunagy.demand.backend.VO.response.DictValueResponseVO;
import com.sunagy.demand.backend.entity.DictType;
import com.sunagy.demand.backend.entity.DictValue;
import com.sunagy.demand.backend.repository.DictTypeRepository;
import com.sunagy.demand.backend.repository.DictValueRepository;
import com.sunagy.demand.backend.service.DictService;
import com.sunagy.demand.backend.utils.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictValueRepository dictValueRepository;
    @Autowired
    private DictTypeRepository dictTypeRepository;

    @Override
    @Transactional
    public DictValueResponseVO addDictValue(DictValueAddRequestVO dictValueAddRequestVO) {
        DictValueResponseVO dictValueResponseVO = new DictValueResponseVO();
        DictValue dictValue = new DictValue();
        BeanUtil.copyProperties(dictValue,dictValueAddRequestVO);
        BeanUtils.copyProperties(dictValueRepository.save(dictValue), dictValueResponseVO);
        return dictValueResponseVO;
    }

    @Override
    @Transactional
    public Integer deleteDictValue(Integer id) {
        dictValueRepository.delete(id);
        return id;
    }

    @Override
    public List<DictTypeResponseVO> getList() {
        List<DictType> dictTypeList = dictTypeRepository.findAll();
        List<DictTypeResponseVO> DictTypeResponseVOList = (List<DictTypeResponseVO>) BeanUtil.copyPropertiesList(dictTypeList,DictTypeResponseVO.class);
        return DictTypeResponseVOList;
    }

    @Override
    public List<DictValueResponseVO> getDictValueList(Integer id) {
        List<DictValue> dictValueList = dictValueRepository.findByTypeId(id);
        List<DictValueResponseVO> dictValueResponseVOList = (List<DictValueResponseVO>) BeanUtil.copyPropertiesList(dictValueList,DictValueResponseVO.class);
        return dictValueResponseVOList;
    }

    @Override
    @Transactional
    public Integer deleteDictType(Integer id) {
        dictTypeRepository.delete(id);
        return id;
    }

    @Override
    @Transactional
    public DictTypeResponseVO addDictType(DictTypeAddRequestVO dictTypeAddRequestVO) {
        DictTypeResponseVO dictTypeResponseVO = new DictTypeResponseVO();
        DictType dictType = new DictType();
        BeanUtil.copyProperties(dictType,dictTypeAddRequestVO);
        BeanUtils.copyProperties(dictTypeRepository.save(dictType), dictTypeResponseVO);
        return dictTypeResponseVO;
    }

    @Override
    @Transactional
    public DictValueResponseVO editDictValue(Integer id,DictValueEditRequestVO dictValueEditRequestVO) {
        DictValueResponseVO dictValueResponseVO = new DictValueResponseVO();
        DictValue dictValue = dictValueRepository.findOne(id);
        BeanUtil.copyProperties(dictValue,dictValueEditRequestVO);
        BeanUtils.copyProperties(dictValueRepository.save(dictValue),dictValueResponseVO);
        return dictValueResponseVO;
    }
}
